package com.yxw.live_vod_boot.service.impl;



import lombok.extern.slf4j.Slf4j;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import com.obs.services.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxw.live_vod_boot.entity.YxLiveAppealEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopPunishEntity;
import com.yxw.live_vod_facade.dto.YxLiveAppealDTO;
import com.yxw.live_vod_boot.dto.YxLiveAppealAddDTO;
import com.yxw.live_vod_boot.dto.YxLiveAppealQueryDTO;
import com.yxw.live_vod_boot.mapper.YxLiveAppealMapper;
import com.yxw.live_vod_boot.service.IYxLiveAppealService;
import com.yxw.live_vod_boot.mapper.YxLiveShopPunishMapper;
import com.yxw.live_vod_boot.utils.HuaWeiObsUtil;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.enums.FileClassifyEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import java.io.IOException;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile; 


/*** 
*@author Jiaxu.Huang
*@date 2023/12/11 16:42:50
*@update 2023/12/11 16:42:50
**/
@Slf4j
@Service
//public class YxLiveAppealServiceImpl  extends ServiceImpl<YxLiveAppealMapper, YxLiveAppealEntity> implements IYxLiveAppealService{
public class YxLiveAppealServiceImpl  implements IYxLiveAppealService{
    /**
     * 图片文件最大大小
     */
    @Value("${file.image.max.size}")
    private long fileImageMaxSize;
    @Autowired
    private HuaWeiObsUtil huaWeiObsUtil;    
    @Autowired
    private YxLiveAppealMapper oYxLiveAppealMapper;
    
    @Autowired
    private YxLiveShopPunishMapper oYxLiveShopPunishMapper;
    /*** 
    * 店铺申诉
    * @param oYxLiveAppealDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult insertYxLiveAppeal(MultipartFile appealPicture,YxLiveAppealAddDTO oYxLiveAppealAddDTO)
    {   
        YxLiveShopPunishEntity oYxLiveShopPunishEntity = oYxLiveShopPunishMapper.selectById(oYxLiveAppealAddDTO.getPunishId());
        if(null == oYxLiveShopPunishEntity)
        {
            return BaseResult.failed("根据id找不到处罚记录");
        }
        //申诉图片大小校验
        if(!appealPicture.isEmpty())
        {
            if (appealPicture.getSize() > fileImageMaxSize) {
                return BaseResult.failed("申诉图片不能上传超过" + fileImageMaxSize / 1048576.0 + "M<br>");
            }
        }
        YxLiveAppealEntity oYxLiveAppealEntity = oYxLiveAppealAddDTO.ConverToEntity();
        String appealPictureKey = "";
        if(!appealPicture.isEmpty())
        {
            String obsDownloadUrlStartPoint = huaWeiObsUtil.getObsDownloadUrlStartPointNoEndSlash();
            //appealPictureKey = FileClassifyEnum.LIVE_FILE.getPath() + BaseDateTime.getShortDateStringNoSlipt() + "/" + UUID.randomUUID().toString().replace("-", "") + appealPicture.getOriginalFilename().substring(appealPicture.getOriginalFilename().lastIndexOf(CommonConstant.POINT));
            appealPictureKey = UUID.randomUUID().toString().replace("-", "") + appealPicture.getOriginalFilename().substring(appealPicture.getOriginalFilename().lastIndexOf(CommonConstant.POINT));
            if(appealPictureKey.startsWith("/"))
            {
                oYxLiveAppealEntity.setPictureAppealUrl(obsDownloadUrlStartPoint.concat(appealPictureKey));
            }
            else
            {
                oYxLiveAppealEntity.setPictureAppealUrl(obsDownloadUrlStartPoint.concat("/").concat(appealPictureKey));
            }
        }
        oYxLiveAppealEntity.setCreateTime(BaseDateTime.getDate());
        oYxLiveAppealEntity.setUpdateTime(BaseDateTime.getDate());
        oYxLiveAppealMapper.insert(oYxLiveAppealEntity);
        oYxLiveShopPunishEntity.setAppealId(oYxLiveAppealEntity.getId());
        oYxLiveShopPunishEntity.setAppealState(0);
        oYxLiveShopPunishMapper.updateById(oYxLiveShopPunishEntity);
        if(!appealPicture.isEmpty())
        {
            //申诉的图片上传华为云            
            try
            {  
                PutObjectResult oPutObjectResult = huaWeiObsUtil.uploadFileInputStream(appealPictureKey, appealPicture.getInputStream());
                log.error("申诉图片oPutObjectResult.getStatusCode()：" + oPutObjectResult.getStatusCode());
            }
            catch (IOException e)
            {  
                log.error("申诉图片上传异常："+e.getMessage());
                throw new BaseException("申诉图片上传："+e.getMessage());
            } 
        }        
        return BaseResult.success(oYxLiveAppealEntity);
    }
    /*** 
    * 管理员处理申诉
    * @param oYxLiveAppealDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult updateYxLiveAppealById(YxLiveAppealDTO oYxLiveAppealDTO)
    {
        YxLiveAppealEntity oYxLiveAppealEntity = oYxLiveAppealMapper.selectById(oYxLiveAppealDTO.getId());
        if(null == oYxLiveAppealEntity)
        {
            return BaseResult.failed("根据Id找不到申诉记录，处理失败");
        }
        YxLiveShopPunishEntity oYxLiveShopPunishEntity = oYxLiveShopPunishMapper.selectById(oYxLiveAppealEntity.getPunishId());
        if(null == oYxLiveShopPunishEntity)
        {
            return BaseResult.failed("根据id找不到处罚记录");
        }        
        oYxLiveAppealEntity.setStatus(oYxLiveAppealDTO.getStatus());
        oYxLiveAppealEntity.setHandleUserId(oYxLiveAppealDTO.getHandleUserId());
        oYxLiveAppealEntity.setAppealUserName(oYxLiveAppealDTO.getHandleUserName());
        oYxLiveAppealEntity.setUpdateTime(BaseDateTime.getDate());
        oYxLiveShopPunishEntity.setAppealState(oYxLiveAppealDTO.getStatus());
        oYxLiveAppealMapper.updateById(oYxLiveAppealEntity);
        oYxLiveShopPunishMapper.updateById(oYxLiveShopPunishEntity);
        return BaseResult.success();
    }
    /*** 
    * 删除
    * @param id
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult deleteYxLiveAppealById(Long id)
    {
        if(ObjectUtil.isEmpty(id))
        {
            return BaseResult.failed("Id不能为空值");
        }
        YxLiveAppealEntity oYxLiveAppealEntity = oYxLiveAppealMapper.selectById(id);
        if(null == oYxLiveAppealEntity)
        {
            return BaseResult.failed("根据Id找不到申诉记录，处理失败");
        } 
        if(oYxLiveAppealEntity.getStatus() != 0)
        {
            return BaseResult.failed("不能删除已审核过的申诉");
        }
        oYxLiveAppealMapper.deleteById(id);
        return BaseResult.success();
    }
    /*** 
    * 根据id查找
    * @param id
    * @return YxLiveAppealEntity
    **/
    @Override
    public YxLiveAppealEntity selectYxLiveAppealById(Long id)
    {
        if(ObjectUtil.isEmpty(id))
        {
            return null;
        }
        return oYxLiveAppealMapper.selectById(id);
    }
    /*** 
    * 自定义查询
    * @param oYxLiveAppealDTO
    * @return List<YxLiveAppealEntity>
    **/
    @Override
    public List<YxLiveAppealEntity> selectYxLiveAppealList(YxLiveAppealQueryDTO oYxLiveAppealQueryDTO)
    {
        QueryWrapper<YxLiveAppealEntity> oYxLiveAppealQueryWrapper = new QueryWrapper();
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getLiveShopId()),"live_shop_id",oYxLiveAppealQueryDTO.getLiveShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopId()),"shop_id",oYxLiveAppealQueryDTO.getShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getStatus()),"status",oYxLiveAppealQueryDTO.getStatus());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getMerchantName()),"merchant_name",oYxLiveAppealQueryDTO.getMerchantName());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopName()),"shop_name",oYxLiveAppealQueryDTO.getShopName());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeStart()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeStart());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeEnd()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeEnd());
        
        return  oYxLiveAppealMapper.selectList(oYxLiveAppealQueryWrapper);
    }
    /*** 
    * 自定义统计
    * @param oYxLiveAppealEntity
    * @return Integer
    **/
    @Override
    public Integer selectYxLiveAppealCount(YxLiveAppealQueryDTO oYxLiveAppealQueryDTO)
    {
        QueryWrapper<YxLiveAppealEntity> oYxLiveAppealQueryWrapper = new QueryWrapper();
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getLiveShopId()),"live_shop_id",oYxLiveAppealQueryDTO.getLiveShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopId()),"shop_id",oYxLiveAppealQueryDTO.getShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getStatus()),"status",oYxLiveAppealQueryDTO.getStatus());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getMerchantName()),"merchant_name",oYxLiveAppealQueryDTO.getMerchantName());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopName()),"shop_name",oYxLiveAppealQueryDTO.getShopName());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeStart()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeStart());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeEnd()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeEnd());
        return oYxLiveAppealMapper.selectCount(oYxLiveAppealQueryWrapper);
    }
    /*** 
    * 自定义分页查询
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oYxLiveAppealEntity
    * @return Page
    **/
    @Override
    public Page selectYxLiveAppealPage(int currentPage,int pageSize,YxLiveAppealQueryDTO oYxLiveAppealQueryDTO)
    {
        if(currentPage <= 1)
        {
            currentPage = 1;
        }
        if(pageSize  <= 0)
        {
            currentPage = 10;
        }
        Page<YxLiveAppealEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxLiveAppealEntity> oYxLiveAppealQueryWrapper = new QueryWrapper();
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getLiveShopId()),"live_shop_id",oYxLiveAppealQueryDTO.getLiveShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopId()),"shop_id",oYxLiveAppealQueryDTO.getShopId());
        oYxLiveAppealQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getStatus()),"status",oYxLiveAppealQueryDTO.getStatus());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getMerchantName()),"merchant_name",oYxLiveAppealQueryDTO.getMerchantName());
        oYxLiveAppealQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getShopName()),"shop_name",oYxLiveAppealQueryDTO.getShopName());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeStart()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeStart());
        oYxLiveAppealQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAppealQueryDTO.getCreateTimeEnd()),"create_time",oYxLiveAppealQueryDTO.getCreateTimeEnd());
        return oYxLiveAppealMapper.selectPage(pageParams, oYxLiveAppealQueryWrapper);
    }
}
    

